package com.zhenzhang.myblog.dao;

import com.zhenzhang.myblog.entity.Comment;
import com.zhenzhang.myblog.entity.Reply;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

public interface CommentDao {
    @Select("SELECT * FROM comment WHERE aid = #{aid} LIMIT #{offset}, #{size}")
    List<Comment> getCommentListByAid(@Param("aid") Integer aid,
                                      @Param("size") Integer size,
                                      @Param("offset") Integer offset);

    @Select("SELECT * FROM comment WHERE uid = #{uid} LIMIT #{offset}, #{size}")
    List<Comment> getCommentListByUid(@Param("uid") Integer uid,
                                      @Param("size") Integer size,
                                      @Param("offset") Integer offset);

    @Select("SELECT * FROM reply WHERE cid = #{cid} LIMIT #{offset}, #{size}")
    List<Reply> getReplyListByCid(@Param("cid") Integer cid,
                                  @Param("size") Integer size,
                                  @Param("offset") Integer offset);

    @Select("SELECT * FROM reply WHERE uid = #{uid} LIMIT #{offset}, #{size}")
    List<Reply> getReplyListByUid(@Param("uid") Integer uid,
                                  @Param("size") Integer size,
                                  @Param("offset") Integer offset);

    @Select("SELECT COUNT(*) FROM comment WHERE aid = #{aid}")
    int getCommentListCountByAid(Integer aid);

    @Select("SELECT COUNT(*) FROM comment WHERE uid = #{uid}")
    int getCommentListCountByUid(Integer uid);

    @Select("SELECT COUNT(*) FROM reply WHERE cid = #{cid}")
    int getReplyListCountByCid(Integer cid);

    @Select("SELECT COUNT(*) FROM reply WHERE uid = #{uid}")
    int getReplyListCountByUid(Integer uid);

    @Insert("INSERT INTO comment " +
            "(`id`,`aid`,`uid`,`content`,`created_at`,`likes`) " +
            "VALUES " +
            "(#{id},#{aid},#{uid},#{content},NOW(),#{likes})")
    void addComment(Comment comment);

    @Insert("INSERT INTO reply " +
            "(`id`,`aid`,`cid`,`uid`,`content`,`created_at`,`likes`,`buser`) " +
            "VALUES " +
            "(#{id},#{aid},#{cid},#{uid},#{content},NOW(),#{likes},#{buser})")
    void addReply(Reply reply);

    @Delete("DELETE FROM comment WHERE id = #{id}")
    void deleteComment(Integer id);

    @Delete({"<script> " +
            " DELETE FROM comment WHERE id IN " +
            " <foreach collection='list' item='item' separator=',' open='(' close=')'>" +
            " #{item} " +
            " </foreach> "+
            " </script>"})
    void batchDeleteComment(ArrayList<Integer> list);

    @Delete("DELETE FROM reply WHERE id = #{id}")
    void deleteReply(Integer id);

    @Delete({"<script> " +
            " DELETE FROM reply WHERE id IN " +
            " <foreach collection='list' item='item' separator=',' open='(' close=')'>" +
            " #{item} " +
            " </foreach> "+
            " </script>"})
    void batchDeleteReply(ArrayList<Integer> list);

    @Update("UPDATE comment SET likes = (likes + 1) WHERE id = #{id}")
    void thumbsUp(Integer id);

    @Update("UPDATE reply SET likes = (likes + 1) WHERE id = #{id}")
    void thumbsUpReply(Integer id);

    @Select("SELECT likes FROM comment WHERE id = #{id}")
    int getLikes(Integer id);

    @Select("SELECT likes FROM reply WHERE id = #{id}")
    int getReplyLikes(Integer id);

    @Select("SELECT MAX(id) FROM comment")
    Integer getMaxId();

    @Select("SELECT MAX(id) FROM reply")
    Integer getReplyMaxId();
}
